package com.hgxd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hgxd.annotation.OperationLogAnnotation;
import com.hgxd.entity.pojo.SysUser;
import com.hgxd.entity.res.ResUserVo;
import com.hgxd.entity.vo.pagevo.SysUserPageVo;
import com.hgxd.result.Result;
import com.hgxd.result.ResultCodeEnum;
import com.hgxd.service.SysUserService;
import com.hgxd.utils.security.MD5;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 系统用户 前端控制器
 * </p>
 *
 * @author fsd
 * @since 2023-10-18
 */
@Api(tags = "用户管理接口")
@RestController
@RequestMapping("/api/sys-user")
@CrossOrigin
public class SysUserController {
    @Resource(name = "SysUserServiceImpl")
    private SysUserService sysUserService;

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "用户的新增接口", notes = "根据传的user来新增，必填,username不可重复")
    @ApiImplicitParam(name = "sysUser", value = "用户的实体类", required = true)
    @OperationLogAnnotation(operModule = "系统用户-新增",operType = "新增",operContent = "新增系统用户")
    public Result insert(@RequestBody SysUser sysUser) {
        if (StringUtils.isBlank(sysUser.getUsername()) || StringUtils.isBlank(sysUser.getPassword())) {
            return Result.fail("参数为空", ResultCodeEnum.ARGUMENT_VALID_ERROR);
        }
        LambdaQueryWrapper<SysUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysUser::getUsername, sysUser.getUsername());
        List<SysUser> list = sysUserService.list(lambdaQueryWrapper);
        if (list.size() != 0) {
            return Result.fail("用户名重复");
        } else {
            sysUser.setCreateTime(LocalDateTime.now());
            sysUser.setUpdateTime(LocalDateTime.now());
            sysUser.setPassword(MD5.encrypt(sysUser.getPassword()));
            sysUserService.save(sysUser);
            return Result.ok(sysUserService.list(lambdaQueryWrapper));
        }
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "更新用户的接口", notes = "根据传的sysUser来修改更新用户")
    @OperationLogAnnotation(operModule = "系统用户-更新",operType = "更新",operContent = "更新系统用户")
    @ApiImplicitParam(name = "sysUser", value = "sysUser类", required = true)
    public Result update(@RequestBody SysUser sysUser) {
        if (sysUser.getUserId() == null) {
            return Result.fail("id为空");
        } else {
            sysUser.setCreateTime(LocalDateTime.now());
            return Result.ok(sysUserService.updateById(sysUser));
        }
    }

    /**
     * 根据id删除
     */
    @PostMapping("/delete/{id}")
    @ApiOperation(value = "删除用户的接口", notes = "根据传的id来删除用户")
    @OperationLogAnnotation(operModule = "系统用户-删除",operType = "删除",operContent = "删除系统用户")
    @ApiImplicitParam(name = "id", value = "用户的id", required = true)
    public Result<Boolean> delete(@PathVariable Integer id) {
        return Result.ok(sysUserService.removeById(id));
    }

    /**
     * 批量删除
     */
    @PostMapping("/del/batch")
    @ApiOperation(value = "批量删除用户的接口", notes = "根据传的id数组来删除用户")
    @ApiImplicitParam(name = "ids", value = "id的list集合", required = true)
    @OperationLogAnnotation(operModule = "系统用户-批量删除",operType = "删除",operContent = "批量删除系统用户")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        boolean data = sysUserService.removeByIds(ids);
        return data ? Result.ok("批量删除成功") : Result.fail("批量删除失败");
    }

    /**
     * 查询全部
     */
    @GetMapping("/getAll")
    @ApiOperation(value = "查询所有用户的接口", notes = "查询全部用户")
    @OperationLogAnnotation(operModule = "系统用户-查询全部",operType = "查询",operContent = "查询全部系统用户")
    public Result<List<ResUserVo>> findAll() {
        return Result.ok(sysUserService.findAllUsers());
    }

    /**
     * 根据id查询一个
     */
    @GetMapping("getById/{id}")
    @ApiOperation(value = "根据id查询一个用户的接口", notes = "根据id查询一个用户")
    @ApiImplicitParam(name = "id", value = "用户id", required = true)
    @OperationLogAnnotation(operModule = "系统用户-根据id查询一个",operType = "查询",operContent = "根据id查询一个系统用户")
    public Result<SysUser> findOne(@PathVariable Integer id) {
        return Result.ok(sysUserService.getById(id));
    }

    /**
     * 根据username查询
     * @param username
     * @return
     */
    @GetMapping("getByUsername/{username}")
    @ApiOperation(value = "根据用户名查询一个用户的接口", notes = "根据用户名查询一个用户")
    @ApiImplicitParam(name = "username", value = "用户名", required = true)
    @OperationLogAnnotation(operModule = "系统用户-根据用户名查询一个",operType = "查询",operContent = "根据用户名查询一个系统用户")
    public Result<List<SysUser>> findOne(@PathVariable String username ) {
        LambdaQueryWrapper<SysUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysUser::getUsername,username);
        return Result.ok(sysUserService.list(lambdaQueryWrapper));
    }

    /**
     * 分页模糊查询
     */
    @PostMapping("/get/page")
    @ApiOperation(value = "分页模糊查询用户", notes = "分页模糊查询")
    @ApiImplicitParam(name = "sysUserPageVo",value = "sysUserPageVo工具类")
    @OperationLogAnnotation(operModule = "系统用户-分页模糊查询",operType = "分页模糊查询",operContent = "分页模糊查询系统用户")
    public Result<Map<String ,Object>> findPage(@RequestBody SysUserPageVo sysUserPageVo) {
        return Result.ok(sysUserService.selectUsersByPage(sysUserPageVo));
    }
}

